import { createRouter, createWebHistory } from 'vue-router';
import { useUserStore } from '../store/user';
import Login from '../views/Login.vue';
import UserList from '../views/UserList.vue';
import RoomTypeList from '../views/RoomTypeList.vue';
import RoomList from '../views/RoomList.vue';
import CustomerList from '../views/CustomerList.vue';
import CheckInList from '../views/CheckInList.vue';
import ExtensionList from '../views/ExtensionList.vue';
import SettlementList from '../views/SettlementList.vue';
import ConsumptionList from '../views/ConsumptionList.vue';

const routes = [
  { path: '/login', component: Login, meta: { requiresAuth: false } },
  { path: '/users', component: UserList, meta: { requiresAuth: true } },
  { path: '/room-types', component: RoomTypeList, meta: { requiresAuth: true } },
  { path: '/rooms', component: RoomList, meta: { requiresAuth: true } },
  { path: '/customers', component: CustomerList, meta: { requiresAuth: true } },
  { path: '/check-ins', component: CheckInList, meta: { requiresAuth: true } },
  { path: '/extensions', component: ExtensionList, meta: { requiresAuth: true } },
  { path: '/settlements', component: SettlementList, meta: { requiresAuth: true } },
  { path: '/consumptions', component: ConsumptionList, meta: { requiresAuth: true } },
  { path: '/', redirect: '/users' }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

router.beforeEach((to, from, next) => {
  const userStore = useUserStore();
  
  if (to.meta.requiresAuth && !userStore.isLogin) {
    next('/login');
  } else if (to.path === '/login' && userStore.isLogin) {
    next('/');
  } else {
    next();
  }
});

export default router; 